package com.example.liunanxin.lnx226ordering.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.beans.ModifyBean;
import com.example.liunanxin.lnx226ordering.iface.ModifyListener;
import com.example.liunanxin.lnx226ordering.model.ModifyModel;


public class ModifyActivity extends AppCompatActivity {
    TextView tv_username;
    EditText et_password,et_phonenum,et_address;
    Button bt_update;
    ImageView iv_return;
    private SharedPreferences sharedPreferences;
    private  int user_id;
    private String password,phonenum,address,username,userpass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);

        initView();
        initData();

        tv_username.setText(username);
        et_password.setText(password);
        et_phonenum.setText(phonenum);
        et_address.setText(address);

        click();
    }

    public void initView(){
        tv_username=findViewById(R.id.textView38);
        et_password=findViewById(R.id.editText10);
        et_phonenum=findViewById(R.id.editText11);
        et_address=findViewById(R.id.editText12);
        bt_update=findViewById(R.id.button7);
        iv_return=findViewById(R.id.imageView34);
    }

    public void initData(){
        user_id=getIntent().getIntExtra("user_id",0);
        username=getIntent().getStringExtra("username");
        phonenum=getIntent().getStringExtra("phone");
        address=getIntent().getStringExtra("address");

        sharedPreferences=getSharedPreferences("password",MODE_PRIVATE);
        password=sharedPreferences.getString("password","fail");//获取了原密码
    }

    public void click(){
       bt_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userpass=et_password.getText().toString();//更新后的密码
                phonenum=et_phonenum.getText().toString();
                address=et_address.getText().toString();

                ModifyModel modifyModel=new ModifyModel();
                modifyModel.updateUserById(user_id,username,userpass,phonenum,address,modifyListener);
            }
        });

       iv_return.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               finish();
           }
       });
    }

    ModifyListener modifyListener=new ModifyListener() {
        @Override
        public void onResponse(ModifyBean modifyBean) {
            if (modifyBean.getSuccess().equals("0")){
                Toast.makeText(ModifyActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(ModifyActivity.this,"修改成功",Toast.LENGTH_SHORT).show();

                if (userpass.equals(password)){
                    Intent intent=new Intent(ModifyActivity.this,HomeActivity.class);
                    startActivity(intent);
                }
                else{
                    Intent intent=new Intent(ModifyActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
            }
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(ModifyActivity.this,mag,Toast.LENGTH_SHORT).show();

        }
    };



}
